package school.valon.rest.services;

import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import school.valon.business.SchoolPolicyBusiness;
import school.valon.dto.SchoolPolicyDto;

@Path("/schoolPolicies")
@PermitAll
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public class SchoolPolicyService {
	@Autowired()
	@Qualifier("schoolPolicyBusiness")
	private SchoolPolicyBusiness schoolPolicyBusiness;

	@POST
	@RolesAllowed("staff")
	@Produces(MediaType.APPLICATION_JSON)
	public Response createSchoolPolicy(SchoolPolicyDto schoolPolicyDto) throws Exception {
		schoolPolicyDto = schoolPolicyBusiness.saveSchoolPolicy(schoolPolicyDto);
		return Response.status(Response.Status.CREATED).entity(schoolPolicyDto).build();
	}

	@PUT
	@Path("{schoolPolicyId}")
	@RolesAllowed("staff")
	public Response updateSchoolPolicy(@PathParam("schoolPolicyId") Integer schoolPolicyId, SchoolPolicyDto schoolPolicyDto) throws Exception {
		schoolPolicyDto.setPolicyId(schoolPolicyId);
		schoolPolicyDto = schoolPolicyBusiness.saveSchoolPolicy(schoolPolicyDto);
		return Response.ok("School Policy updated").build();
	}

	@DELETE
	@Path("{schoolPolicyId}")
	@RolesAllowed("staff")
	public Response deleteSchoolPolicy(@PathParam("schoolPolicyId") Integer schoolPolicyId) throws Exception {
		schoolPolicyBusiness.deleteSchoolPolicy(schoolPolicyId);
		return Response.ok("School Policy deleted").build();
	}
}
